namespace ASPNET.UserControls
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using ASBCC.Business;
    using ASB.CC.Business.ACR;
    using System.Web;
    using System.Text;

    public class DisplayIssues : UserControl
    {
        protected System.Web.UI.WebControls.ImageButton btnPageFirst;
        protected System.Web.UI.WebControls.ImageButton btnPagePrev;
        protected System.Web.UI.WebControls.ImageButton btnPageNext;
        protected System.Web.UI.WebControls.ImageButton btnPageLast;
        protected System.Web.UI.WebControls.Label lblRecordCount;
        protected System.Web.UI.WebControls.Label lblPageCount;
        protected System.Web.UI.WebControls.Label lblCurrentPageIndex;
        protected System.Web.UI.WebControls.ImageButton btnGotoPage;
        protected System.Web.UI.WebControls.TextBox txtPage;

        private string[] _arrIssueColumns;
        [AccessedThroughProperty("btnCancel")]
        private Button _btnCancel;
        [AccessedThroughProperty("btnExport")]
        private Button _btnExport;
        [AccessedThroughProperty("btnSave")]
        private Button _btnSave;
        private IssueCollection _DataSource;
        [AccessedThroughProperty("grdIssues")]
        private DataGrid _grdIssues;
        [AccessedThroughProperty("lblNoIssues")]
        private Label _lblNoIssues;
        [AccessedThroughProperty("lnkSelectColumns")]
        private LinkButton _lnkSelectColumns;
        [AccessedThroughProperty("lstIssueColumns")]
        private CheckBoxList _lstIssueColumns;
        [AccessedThroughProperty("pnlSelectColumns")]
        private Panel _pnlSelectColumns;
        [AccessedThroughProperty("tblOptions")]
        private HtmlTable _tblOptions;
        private object designerPlaceholderDeclaration;

        public event EventHandler RebindCommand;

        public DisplayIssues()
        {
            base.Init += new EventHandler(this.Page_Init);
            this._arrIssueColumns = new string[] { "3", /*"4", "5",*/ "6", /*"7",*/ "8" };
        }

        public void CancelClick(object s, EventArgs e)
        {
            this.pnlSelectColumns.Visible = false;
        }

        public override void DataBind()
        {
            if (this.FN == null)
                this.DataSource = Issue.GetIssuesByProjectId(this.ProjectID);
            this.SortGridData(this._DataSource, this.SortField, this.SortAscending);
            this.grdIssues.DataSource = this._DataSource;
            this.grdIssues.DataBind();
            if (this.grdIssues.Items.Count > 0)
            {
                this.grdIssues.Visible = true;
                this.tblOptions.Visible = true;
                this.lblNoIssues.Visible = false;
                this.DisplayColumns();
            }
            else
            {
                this.grdIssues.Visible = false;
                this.tblOptions.Visible = false;
                this.lblNoIssues.Visible = true;
            }
            SetPageCount(this._DataSource.Count, 1);
        }

        public void DataBind(int pageIndex)
        {
            if (this.FN == null)
                this.DataSource = Issue.GetIssuesByProjectId(this.ProjectID);
            else
            {
                QueryClauseCollection colQueryClauses = new QueryClauseCollection();
                for (int i = 0; i < this.BO.Length; i++)
                {
                    QueryClause objQueryClause = new QueryClause(this.BO[i], this.FN[i], this.CO[i], this.FV[i], this.Sdt[i]);
                    colQueryClauses.Add(objQueryClause);
                }                
                this.DataSource = Issue.PerformQuery(this.ProjectID, colQueryClauses);
            }

            this.SortGridData(this._DataSource, this.SortField, this.SortAscending);
            this.grdIssues.DataSource = this._DataSource;
            this.grdIssues.DataBind();
            if (this.grdIssues.Items.Count > 0)
            {
                this.grdIssues.Visible = true;
                this.tblOptions.Visible = true;
                this.lblNoIssues.Visible = false;
                this.DisplayColumns();
            }
            else
            {
                this.grdIssues.Visible = false;
                this.tblOptions.Visible = false;
                this.lblNoIssues.Visible = true;
            }

            int count = this._DataSource.Count;//Convert.ToInt32(this.lblRecordCount.Text);

            SetPageCount(count, pageIndex);
        }

        public void DisplayColumns()
        {
            int _Vb_t_i4_0 = this.grdIssues.Columns.Count - 1;
            for (int index = 2; index <= _Vb_t_i4_0; index++)
            {
                this.grdIssues.Columns[index].Visible = false;
            }
            foreach (string colIndex in this._arrIssueColumns)
            {
                this.grdIssues.Columns[int.Parse(colIndex)].Visible = true;
            }
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.btnPageFirst.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageFirst_Click);
            this.btnPagePrev.Click += new System.Web.UI.ImageClickEventHandler(this.btnPagePrev_Click);
            this.btnPageNext.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageNext_Click);
            this.btnPageLast.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageLast_Click);
            this.btnGotoPage.Click += new System.Web.UI.ImageClickEventHandler(this.btnGotoPage_Click);
        }

        public void IssuesPageIndexChanged(object s, DataGridPageChangedEventArgs e)
        {
            this.grdIssues.CurrentPageIndex = e.NewPageIndex;
            this.OnRebindCommand(EventArgs.Empty);
        }

        public void IssuesSortCommand(object s, DataGridSortCommandEventArgs e)
        {
            this.SortField = e.SortExpression;
            this.OnRebindCommand(EventArgs.Empty);
        }

        private void lnkSelectColumns_Click(object sender, EventArgs e)
        {
        }

        public void OnRebindCommand(EventArgs e)
        {
            if (this.RebindCommand != null)
            {
                this.RebindCommand(this, e);
            }
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
            this.PageInit();
        }

        public void PageInit()
        {
            if ((this.Request.Cookies["issuecolumns"] != null) && (StringType.StrCmp(this.Request.Cookies["issuecolumns"].Value, string.Empty, false) != 0))
            {
                this._arrIssueColumns = this.Request.Cookies["issuecolumns"].Value.Split(new char[0]);
            }
            if (!CommonHelper.IsACRAdmin((this.Page.User as CSPrincipal).Role2s))
                this.btnExport.Visible = false;
            else
                this.btnExport.Visible = true;
        }

        public void SaveClick(object s, EventArgs e)
        {
            string strIssueColumns = " 0";
            foreach (ListItem item in this.lstIssueColumns.Items)
            {
                if (item.Selected)
                {
                    strIssueColumns = strIssueColumns + " " + item.Value;
                }
            }
            strIssueColumns = strIssueColumns.Trim();
            this._arrIssueColumns = strIssueColumns.Split(new char[0]);
            this.Response.Cookies["issuecolumns"].Value = strIssueColumns;
            this.Response.Cookies["issuecolumns"].Path = "/";
            this.Response.Cookies["issuecolumns"].Expires = DateTime.MaxValue;
            this.pnlSelectColumns.Visible = false;
            this.OnRebindCommand(EventArgs.Empty);
        }

        public void SelectColumnsClick(object s, EventArgs e)
        {
            this.pnlSelectColumns.Visible = true;
            foreach (string colIndex in this._arrIssueColumns)
            {
                ListItem item = this.lstIssueColumns.Items.FindByValue(colIndex);
                if (item != null)
                {
                    item.Selected = true;
                }
            }
        }

        private void SortGridData(IssueCollection list, string sortField, bool asc)
        {
            IssueCollection.IssueFields sortCol = IssueCollection.IssueFields.InitValue;
            string _Vb_t_string_0 = sortField;
            if (StringType.StrCmp(_Vb_t_string_0, "Id", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Id;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Title", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Title;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Category", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Category;
            }
            //else if (StringType.StrCmp(_Vb_t_string_0, "Assigned", false) == 0)
            //{
            //    sortCol = IssueCollection.IssueFields.Assigned;
            //}
            //else if (StringType.StrCmp(_Vb_t_string_0, "Owner", false) == 0)
            //{
            //    sortCol = IssueCollection.IssueFields.Owner;
            //}
            else if (StringType.StrCmp(_Vb_t_string_0, "Creator", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Creator;
            }
            //else if (StringType.StrCmp(_Vb_t_string_0, "Priority", false) == 0)
            //{
            //    sortCol = IssueCollection.IssueFields.Priority;
            //}
            //else if (StringType.StrCmp(_Vb_t_string_0, "Status", false) == 0)
            //{
            //    sortCol = IssueCollection.IssueFields.Status;
            //}
            else if (StringType.StrCmp(_Vb_t_string_0, "Milestone", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Milestone;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Created", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Created;
            }
            if (list != null)
            {
                list.Sort(sortCol, asc);
            }
        }

        protected void btnExportClick(object sender, EventArgs e)
        {
            //HttpContext.Current.Response.Charset = "UTF-8";
            //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("MyCompany.xls", Encoding.UTF8).ToString());
            //HttpContext.Current.Response.ContentType = "application/ms-excel";
            this.grdIssues.AllowPaging = false;
            this.grdIssues.AllowSorting = false;
            DataBind(1);
            BEGDC.CC.Web.Control.ExportDataGridToExcel.Export(this.Page, this.grdIssues, "IssueList.xls");
            //System.IO.StringWriter tw = new System.IO.StringWriter();
            //HtmlTextWriter hw = new HtmlTextWriter(tw);
            //this.grdIssues.RenderControl(hw);
            //HttpContext.Current.Response.Write(tw.ToString());
            //HttpContext.Current.Response.End();
            this.grdIssues.AllowPaging = true;
            this.grdIssues.AllowSorting = true;
            DataBind(1);
        }

        protected virtual Button btnExport
        {
            get
            {
                return this._btnExport;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnExport != null)
                {
                    this._btnCancel.Click -= new EventHandler(this.btnExportClick);
                }
                this._btnExport = value;
                if (this._btnExport != null)
                {
                    this._btnExport.Click += new EventHandler(this.btnExportClick);
                }
            }
        }

        protected virtual Button btnCancel
        {
            get
            {
                return this._btnCancel;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnCancel != null)
                {
                    this._btnCancel.Click -= new EventHandler(this.CancelClick);
                }
                this._btnCancel = value;
                if (this._btnCancel != null)
                {
                    this._btnCancel.Click += new EventHandler(this.CancelClick);
                }
            }
        }

        protected virtual Button btnSave
        {
            get
            {
                return this._btnSave;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnSave != null)
                {
                    this._btnSave.Click -= new EventHandler(this.SaveClick);
                }
                this._btnSave = value;
                if (this._btnSave != null)
                {
                    this._btnSave.Click += new EventHandler(this.SaveClick);
                }
            }
        }

        public int CurrentPageIndex
        {
            get
            {
                return this.grdIssues.CurrentPageIndex;
            }
            set
            {
                this.grdIssues.CurrentPageIndex = value;
            }
        }

        public IssueCollection DataSource
        {
            get
            {
                return this._DataSource;
            }
            set
            {
                this._DataSource = value;
            }
        }

        protected virtual DataGrid grdIssues
        {
            get
            {
                return this._grdIssues;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._grdIssues != null)
                {
                    this._grdIssues.SortCommand -= new DataGridSortCommandEventHandler(this.IssuesSortCommand);
                    this._grdIssues.PageIndexChanged -= new DataGridPageChangedEventHandler(this.IssuesPageIndexChanged);
                }
                this._grdIssues = value;
                if (this._grdIssues != null)
                {
                    this._grdIssues.SortCommand += new DataGridSortCommandEventHandler(this.IssuesSortCommand);
                    this._grdIssues.PageIndexChanged += new DataGridPageChangedEventHandler(this.IssuesPageIndexChanged);
                }
            }
        }

        protected virtual Label lblNoIssues
        {
            get
            {
                return this._lblNoIssues;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lblNoIssues != null)
                {
                }
                this._lblNoIssues = value;
                if (this._lblNoIssues != null)
                {
                }
            }
        }

        protected virtual LinkButton lnkSelectColumns
        {
            get
            {
                return this._lnkSelectColumns;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lnkSelectColumns != null)
                {
                    this._lnkSelectColumns.Click -= new EventHandler(this.lnkSelectColumns_Click);
                    this._lnkSelectColumns.Click -= new EventHandler(this.SelectColumnsClick);
                }
                this._lnkSelectColumns = value;
                if (this._lnkSelectColumns != null)
                {
                    this._lnkSelectColumns.Click += new EventHandler(this.lnkSelectColumns_Click);
                    this._lnkSelectColumns.Click += new EventHandler(this.SelectColumnsClick);
                }
            }
        }

        protected virtual CheckBoxList lstIssueColumns
        {
            get
            {
                return this._lstIssueColumns;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lstIssueColumns != null)
                {
                }
                this._lstIssueColumns = value;
                if (this._lstIssueColumns != null)
                {
                }
            }
        }

        protected virtual Panel pnlSelectColumns
        {
            get
            {
                return this._pnlSelectColumns;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._pnlSelectColumns != null)
                {
                }
                this._pnlSelectColumns = value;
                if (this._pnlSelectColumns != null)
                {
                }
            }
        }

        public bool SortAscending
        {
            get
            {
                object o = RuntimeHelpers.GetObjectValue(this.ViewState["SortAscending"]);
                return ((o == null) || BooleanType.FromObject(o));
            }
            set
            {
                this.ViewState["SortAscending"] = value;
            }
        }

        public string SortField
        {
            get
            {
                object o = RuntimeHelpers.GetObjectValue(this.ViewState["SortField"]);
                if (o == null)
                {
                    return string.Empty;
                }
                return StringType.FromObject(o);
            }
            set
            {
                if (StringType.StrCmp(value, this.SortField, false) == 0)
                {
                    this.SortAscending = !this.SortAscending;
                }
                this.ViewState["SortField"] = value;
            }
        }

        protected virtual HtmlTable tblOptions
        {
            get
            {
                return this._tblOptions;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._tblOptions != null)
                {
                }
                this._tblOptions = value;
                if (this._tblOptions != null)
                {
                }
            }
        }

        #region page navigation

        public string[] BO
        {
            get
            {
                if (ViewState["BO"] != null) return (string[])ViewState["BO"];
                else
                    return null;
            }
            set
            {
                ViewState["BO"] = value;
            }
        }

        public string[] CO
        {
            get
            {
                if (ViewState["CO"] != null) return (string[])ViewState["CO"];
                else
                    return null;
            }
            set
            {
                ViewState["CO"] = value;
            }
        }

        public string[] FN
        {
            get
            {
                if (ViewState["FN"] != null) return (string[])ViewState["FN"];
                else
                    return null;
            }
            set
            {
                ViewState["FN"] = value;
            }
        }

        public string[] FV
        {
            get
            {
                if (ViewState["FV"] != null) return (string[])ViewState["FV"];
                else
                    return null;
            }
            set
            {
                ViewState["FV"] = value;
            }
        }

        public System.Data.SqlDbType[] Sdt
        {
            get
            {
                if (ViewState["SqlDbType"] != null) return (System.Data.SqlDbType[])ViewState["SqlDbType"];
                else
                    return null;
            }
            set
            {
                ViewState["SqlDbType"] = value;
            }
        }

        public int ProjectID
        {
            get
            {
                if (ViewState["ProjectID"] != null) return (int)ViewState["ProjectID"];
                else
                    return 0;
            }
            set
            {
                ViewState["ProjectID"] = value;
            }
        }

        private int _PageCount
        {
            get
            {
                if (ViewState["PageCount"] != null) return (int)ViewState["PageCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["PageCount"] = value;
            }
        }

        private int _CurrentPageIndex
        {
            get
            {
                if (ViewState["CurrentPageIndex"] != null) return (int)ViewState["CurrentPageIndex"];
                else
                    return 0;
            }
            set
            {
                ViewState["CurrentPageIndex"] = value;
            }
        }

        private int _RowCount
        {
            get
            {
                if (ViewState["RowCount"] != null) return (int)ViewState["RowCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["RowCount"] = value;
            }
        }

        private void SetPageNavi(int newPage)
        {
            if (newPage <= 1)
            {

                this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first_inact.gif";
                this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre_inact.gif";
                this.btnPageFirst.Attributes["disabled"] = "true";
                this.btnPagePrev.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first.gif";
                this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre.gif";
                this.btnPageFirst.Attributes.Remove("disabled");
                this.btnPagePrev.Attributes.Remove("disabled");
            }

            if (newPage >= _PageCount)
            {
                this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next_inact.gif";
                this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last_inact.gif";
                this.btnPageNext.Attributes["disabled"] = "true";
                this.btnPageLast.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next.gif";
                this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last.gif";
                this.btnPageNext.Attributes.Remove("disabled");
                this.btnPageLast.Attributes.Remove("disabled");
            }

            this.lblCurrentPageIndex.Text = newPage.ToString();
            this.lblPageCount.Text = _PageCount.ToString();
            this.lblRecordCount.Text = _RowCount.ToString();
        }

        private void SetPageCount(int count, int pageIndex)
        {
            if (count > 0)
            {
                _RowCount = count;
                _PageCount = count / this.grdIssues.PageSize;
                if (count % this.grdIssues.PageSize > 0) _PageCount++;

                _CurrentPageIndex = pageIndex;
                SetPageNavi(pageIndex);
            }
            else
            {
                _RowCount = count;
                _PageCount = count;
                _CurrentPageIndex = 0;
                SetPageNavi(0);
            }
        }

        #endregion

        #region ---------- paging events ----------------

        private void btnPageFirst_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.grdIssues.CurrentPageIndex = 0;
            this.DataBind(1);
        }

        private void btnPagePrev_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_CurrentPageIndex == 1) return;
            this.grdIssues.CurrentPageIndex = _CurrentPageIndex - 2;
            this.DataBind(_CurrentPageIndex - 1);
        }

        private void btnPageNext_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_CurrentPageIndex == _PageCount) return;
            this.grdIssues.CurrentPageIndex = _CurrentPageIndex;
            this.DataBind(_CurrentPageIndex + 1);
        }

        private void btnPageLast_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.grdIssues.CurrentPageIndex = _PageCount - 1;
            this.DataBind(_PageCount);
        }

        private void btnGotoPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (this.txtPage.Text == string.Empty)
            {
                this.txtPage.Text = "1";
            }
            int gotopage = 1;
            try
            {
                gotopage = Convert.ToInt32(this.txtPage.Text);
            }
            catch { }
            int currentpage = 0;

            if (gotopage == 0) return;
            if (_PageCount == 0) return;

            if (gotopage >= _PageCount)
            {
                gotopage = _PageCount;
                currentpage = gotopage - 1;
                if (currentpage < 0) currentpage = 0;
            }
            else if (gotopage <= 0)
            {
                gotopage = 1;
            }
            else
            {
                currentpage = gotopage - 1;
            }

            this.grdIssues.CurrentPageIndex = currentpage;

            this.DataBind(gotopage);

            this.txtPage.Text = string.Empty;
        }

        #endregion
    }
}

